package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限-用户 controller
 */

@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码", required = true,dataType = "Long"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数", required = true,dataType = "Long"),
            @ApiImplicitParam(name = "UserDto",value = "用户DTO", required = true,dataType = "UserDto"),
    })
    public ResponseResult<PageResponse<UserVo>> getByPage(@RequestBody UserDto userDto,
                                                         @PathVariable Integer pageNum, @PathVariable Integer pageSize){
        log.info("分页条件查询，页码：{}，条数：{}，条件：{}",pageNum,pageSize,userDto);
        PageResponse<UserVo> result = userService.getPage(pageNum,pageSize,userDto);
        return ResponseResult.success(result);
    }

    @PutMapping
    @ApiOperation(value = "添加用户")
    @ApiImplicitParam(name = "UserDto",value = "用户DTO实体类",required = true,dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"UserDto.realName","UserDto.roleVoIds","UserDto.deptNo",
                                              "UserDto.postNo","UserDto.dataState","UserDto.email","UserDto.mobile",})
    public ResponseResult add(@RequestBody UserDto userDto){
        log.info("添加用户{}",userDto.toString());
        userService.addUser(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation(value = "修改用户")
    @ApiImplicitParam(name = "UserDto",value = "用户DTO实体类",required = true,dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"UserDto.realName","UserDto.roleVoIds","UserDto.deptNo",
                                              "UserDto.postNo","UserDto.dataState","UserDto.email","UserDto.mobile",})
    public ResponseResult updateUser(@RequestBody UserDto userDto){
        log.info("修改用户{}",userDto.toString());
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "启用-禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "用户ID",value = "id",dataType = "Long",required = true),
            @ApiImplicitParam(paramType = "path",name = "用户状态",value = "status",dataType = "String",required = true)
    })
    public ResponseResult changeStatus(@PathVariable Long id,@PathVariable String status){
        log.info("修改用户 {} 的状态为：{}",id,status);
        userService.updateUserStatus(id,status);
        return ResponseResult.success();
    }

    @PutMapping("/list")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParam(name = "用户姓名",value = "realName",dataType = "String",required = true)
    public ResponseResult<UserVo> getUserList(String realName){
        log.info("用户{}的列表",realName);
        List<UserVo> userVo = userService.getUserList(realName);
        return ResponseResult.success(userVo);
    }

    @DeleteMapping("/remove/{userId}")
    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(paramType = "path",name = "用户ID",value = "userId",dataType = "String",required = true)
    public ResponseResult removeUser(@PathVariable String userId){
        log.info("删除用户{}",userId);
        userService.removeUser(userId);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "重置密码")
    @ApiImplicitParam(paramType = "path",name = "用户ID",value = "userId",dataType = "String",required = true)
    public ResponseResult resetPassword(@PathVariable String userId){
        log.info("重置用户{}的密码",userId);
        userService.resetPassword(userId);
        return ResponseResult.success();
    }



}
